package trycat.cn.pagelistview;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: trycat <br>
 * Description: Unicorn <br>
 * Since: 2017/3/14 10:48 <br>
 */

public class SignCardFragment extends Fragment {

    private TurnCardListView cardList;
    private CardAdapter adapter;
    private List<String> list = new ArrayList<>();

    public SignCardFragment() {
    }

    public static Fragment newInstance() {
        return new SignCardFragment();
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.sign_card_fragment, container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        cardList = (TurnCardListView) view.findViewById(R.id.cardList);

        adapter = new CardAdapter(list);
        cardList.setAdapter(adapter);

    }

    class CardAdapter extends BaseAdapter {
        List<String> list;

        public CardAdapter(List<String> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            ViewHolder viewHolder;
            if (view == null) {
                view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_sign_card_fragment, viewGroup, false);
                viewHolder = new ViewHolder(view);
                view.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) view.getTag();
            }
            viewHolder.nameTV.setText(list.get(i));
//            viewHolder.addressTV.setText(vo.getAddress());
//            viewHolder.timeTV.setText(vo.getStartTime());
            viewHolder.statusTV.setText("活动进行中");
            if (i % 2 == 0) {
                viewHolder.submitTV.setText("开 始");
            } else {
                viewHolder.submitTV.setText("查看信息");
            }
            return view;
        }

        class ViewHolder {

            ImageView image;
            TextView nameTV, addressTV, timeTV, statusTV, submitTV;

            public ViewHolder(View view) {
                image = (ImageView) view.findViewById(R.id.image);
                nameTV = (TextView) view.findViewById(R.id.nameTV);
                addressTV = (TextView) view.findViewById(R.id.addressTV);
                timeTV = (TextView) view.findViewById(R.id.timeTV);
                statusTV = (TextView) view.findViewById(R.id.statusTV);
                submitTV = (TextView) view.findViewById(R.id.submitTV);
            }
        }
    }
    public void setData(List<String> list) {

        if (!this.list.isEmpty()) {
            this.list.clear();
        }
        this.list.addAll(list);
        adapter.notifyDataSetChanged();
    }
}
